package br.com.dao;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import br.com.dao.util.GenericDao;
import br.com.model.TipoMidia;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class TipoMidiaDao implements GenericDao<TipoMidia> {

    private Connection connection;

    public TipoMidiaDao(Connection connection) {
        this.connection = connection;
    }

    public TipoMidiaDao() {
    }

    @Override
    public void save(TipoMidia tipoMidia) {

        try {
            Integer idTipoMidia;

            String sql = "insert into tipoMidia"
                    + "(nome,valorpadraolocacao,ativo)"
                    + " values (?,?,?)";

            PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.setString(1, tipoMidia.getNome());
            stmt.setDouble(2, tipoMidia.getValorPadraoLocacao());
            stmt.setBoolean(3, tipoMidia.isAtivo());

            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(TipoMidia tipoMidia) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public LinkedList<TipoMidia> listar() {

        LinkedList<TipoMidia> lista = new LinkedList<TipoMidia>();

        String sql = "select nome, valorpadraolocacao, ativo, idTipoMidia FROM tipoMidia";

        PreparedStatement stmt;
        TipoMidia temp;

        try {
            stmt = this.connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                temp = new TipoMidia(rs.getString(1), rs.getDouble(2), rs.getBoolean(3), rs.getInt(4));
                lista.add(temp);

            }


        } catch (SQLException ex) {
            Logger.getLogger(TipoMidiaDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return lista;
    }

    @Override
    public TipoMidia getObjectById(Integer id) {
        String sql = "select nome, valorpadraolocacao, ativo, idTipoMidia FROM tipomidia WHERE idTipoMidia = '" + id + "'";

        PreparedStatement stmt;
        TipoMidia temp;
        try {
            stmt = this.connection.prepareStatement(sql);

            ResultSet rs = stmt.executeQuery();
            rs.next();
            temp = new TipoMidia(rs.getString(1), rs.getDouble(2), rs.getBoolean(3), rs.getInt(4));

            return temp;
        } catch (SQLException ex) {
            Logger.getLogger(TipoMidiaDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    @Override
    public Collection<TipoMidia> list() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public TipoMidia findById(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public LinkedList<TipoMidia> getObjectByName(String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
